package com.wuba.baojie.controller;

import com.wuba.baojie.config.ExcleUtil;
import com.wuba.baojie.dto.*;
//import com.wuba.baojie.dto.VideorDto;
import com.wuba.baojie.enums.ErrorEnums;
import com.wuba.baojie.pojo.Apply;
import com.wuba.baojie.reponse.BaseResult;
import com.wuba.baojie.reponse.RetResponse;
import com.wuba.baojie.service.WeChatService;
import com.wuba.baojie.vo.ApplyListVo;
import com.wuba.baojie.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @Author: Eric.zhou
 * @Date: 2019/6/3 14:33
 */
@RestController
@Api("微信用户控制")
@RequestMapping("/wechat/user")
@CrossOrigin
public class WeChatController {


    @Autowired
    private WeChatService weChatService;

    @ApiOperation("登录")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "code", value = "微信code", required = true, example = "1")
    })
    @ResponseBody
    public BaseResult login(@NotEmpty(message = "参数不能为空") @RequestParam("code") String code) throws URISyntaxException, IOException {
        try {
            LoginVo loginVo = weChatService.weChatLogin(code);
            return RetResponse.makeOKRsp(loginVo);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(ErrorEnums.ERROR_SYSTEM.getEmsg());
        }
    }

    @ApiOperation("签到")
    @PostMapping("/sign")
    @ResponseBody
    public BaseResult sign(@RequestBody SignDto signDto) throws URISyntaxException, IOException {
        try {
           int num =  weChatService.sign(signDto);
            return RetResponse.makeOKRsp(num);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("获取积分")
    @PostMapping("/scoring")
    @ResponseBody
    public BaseResult scoring(@RequestBody ScoringDto scoringDto) throws URISyntaxException, IOException {
        try {
            weChatService.scoring(scoringDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    @ApiOperation("获取手机号")
    @PostMapping("/getPhoneNumber")
    @ResponseBody
    public BaseResult getPhoneNumber(@RequestBody PhoneNumberDto phoneNumberDto) throws URISyntaxException, IOException {
        try {
            Object phoneNumber = weChatService.getPhoneNumber(phoneNumberDto);
            return RetResponse.makeOKRsp(phoneNumber);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("获取视频")
    @PostMapping("/getVideo")
    @ResponseBody
    public BaseResult getVideo(@RequestBody VideoDto videoDto) throws URISyntaxException, IOException {
        try {
            String url = weChatService.getVideo(videoDto);
            return RetResponse.makeOKRsp(url);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("申报资料")
    @PostMapping("/apply")
    @ResponseBody
    public BaseResult apply(@RequestBody ApplyDto applyDto) throws URISyntaxException, IOException {
        try {
             weChatService.apply(applyDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("申报资料查询")
    @GetMapping("/applyList")
    @ResponseBody
    public BaseResult applyList(String address) throws URISyntaxException, IOException {
        try {
           Apply apply =  weChatService.applyList(address);
            return RetResponse.makeOKRsp(apply);
        } catch (Exception e) {
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    @ApiOperation("申报资料查询")
    @GetMapping("/applyListExport")
    public void applyListExport(HttpServletResponse httpServletResponse) throws URISyntaxException, IOException {
            List<Apply> lists =  weChatService.applyLists();
            ExcleUtil.exportExcel(lists,"物资统计","sheet1",Apply.class,"物资统计.xls",httpServletResponse);
    }



    /**
     * 接收二维码
     * @param request
     * @return
     * @throws IOException
     */
    @ApiOperation("接收二维码")
    @GetMapping("/toCode")
//    @ResponseBody
    public void twoCode(HttpServletResponse res,String sences) throws Exception {
        weChatService.getQRCode(res,sences);

    }

    /**
     * 接收二维码
     * @param request
     * @return
     * @throws IOException
     */
    @ApiOperation("打卡列表")
    @GetMapping("/signList")
//    @ResponseBody
    public void signList(HttpServletResponse httpServletResponse,String start,String end) throws Exception {
       List<SignListVo> listVos =  weChatService.signList(start,end);
        ExcleUtil.exportExcel(listVos,"打卡列表","sheet1",SignListVo.class,"打卡列表"+start+"-"+end+".xls",httpServletResponse);
    }






}
